package org.urey.fmp.producer.processor;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Date;

import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import com.fasterxml.jackson.databind.ObjectMapper;

public class CustomMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
	
	public CustomMappingJackson2HttpMessageConverter() {
		super();
	}
	
	public CustomMappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
		super(objectMapper);
	}

	@Override
	public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
		if ("java.util.Date".equals(type.getTypeName())) {
			return new Date(Long.valueOf(IOUtils.toString(inputMessage.getBody(), "UTF-8")));
		}
		return super.read(type, contextClass, inputMessage);
	}

}
